import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yp/config/index.dart';
import 'package:yp/router.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/index.dart';

class AddressList extends StatefulWidget {
  const AddressList({Key key}) : super(key: key);

  @override
  _AddressListState createState() => _AddressListState();
}

class _AddressListState extends State<AddressList>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  List<Map<String, dynamic>> _list = [
    {'id': 0, 'iSselected': true},
    {'id': 1, 'iSselected': false},
    {'id': 2, 'iSselected': false}
  ];

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);

    print(['', ''].toList());
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: YPNavigationBar('选择收货地址'),
      body: Container(
        padding:
            EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom + 66),
        child: ListView.separated(
            padding: EdgeInsets.only(bottom: 20),
            itemBuilder: (context, index) => _adderssItem(_list[index]),
            separatorBuilder: (context, index) => YpLine(),
            itemCount: _list?.length ?? 0),
      ),
      floatingActionButton: _bottom(),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      backgroundColor: EsColors.White,
    );
  }

  /// 地址item
  Widget _adderssItem(Map<String, dynamic> info) {
    return GestureDetector(
      onTap: () {
        setState(() {
          for (var i = 0; i < _list.length; i++) {
            _list[i]['iSselected'] = info['id'] == _list[i]['id'];
          }
        });

        /// 为了看到选中状态 做了延迟 不需要的话 可以去掉
        Future.delayed(Duration(milliseconds: 50), () {
          Get.back();
        });
      },
      child: Padding(
        padding: EdgeInsets.all(12),
        child: Row(
          children: [
            info['iSselected']
                ? Image(
                    image: AssetImage('assets/images/selectIcon1.png'),
                    width: 18,
                  )
                : SizedBox(width: 18),
            SizedBox(width: 8),
            Expanded(
              child: Column(
                children: [
                  Row(
                    children: [
                      Container(
                        constraints: BoxConstraints(
                            maxWidth: MediaQuery.of(context).size.width / 3),
                        child: Text(
                          '大鹅',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              color: EsColors.TitleSecondary,
                              fontSize: EsSizes.MD,
                              fontWeight: EsFontWeights.Medium),
                        ),
                      ),
                      SizedBox(width: 12),
                      Expanded(
                        child: Text(
                          '128****3321',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                              color: EsColors.TitleSecondary,
                              fontSize: EsSizes.MD,
                              fontWeight: EsFontWeights.Medium),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 4),
                  Text(
                    '江苏省 苏州市 昆山市 花桥镇 绿地大道211号 启航城晶彩小区 36号402室',
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: EsColors.TitleSecondary,
                        fontSize: EsSizes.SM,
                        fontWeight: EsFontWeights.Normal),
                  )
                ],
              ),
            ),
            EsIconButton(
              Image(
                image: AssetImage('assets/images/editIcon.png'),
                width: 18,
              ),
              onTap: () {
                print('编辑地址信息');
              },
            )
          ],
        ),
      ),
    );
  }

  /// 下边的按钮
  Widget _bottom() {
    return EsBottomWidget(
        leftTitle: null, rightTitle: '+ 新增收货地址', rightlick: () {
          Get.toNamed(RouteConfig.editAddress);
        });
  }

  ///
}
